﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MO.ModelingClient
{
    public partial class ItemTextControl : ItemBaseControl
    {
        Func<BaseTextEditForm> GetBaseTextEditForm;
        public ItemTextControl(string title, string defaultStr, Func<BaseTextEditForm> getBaseTextEditForm)
        {
            InitializeComponent();
            this.textBox.Text = defaultStr;
            this.GetBaseTextEditForm = getBaseTextEditForm;
            base.Init(title);
        }
        public override void SetValue(object value)
        {
            this.textBox.Text = value as string;
        }
        public override object GetValue()
        {
            return this.textBox.Text.Trim();
        }
        public override void SetEnabled(bool enabled)
        {
            this.textBox.ReadOnly = !enabled;
        }

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.E)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
            }
        }

        private void textBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.E)
            {
                if (this.textBox.ReadOnly)
                    return;
                BaseTextEditForm form;
                if (GetBaseTextEditForm != null)
                    form = GetBaseTextEditForm();
                else
                    form = new TextEditForm();
                //TextEditForm form = new TextEditForm();//new TextEditForm(this.Title, this.textBox.Text.Trim(), SetValue);
                form.Text = this.Title;
                form.SetValue(this.textBox.Text.Trim());
                form.ReturnValue = SetValue;
                form.ShowDialog(this.FindForm());
            }
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (base.ValueChanged != null)
                base.ValueChanged(GetValue());
        }
    }
    
}
